// routes/index.js
const Router = require('koa-router');
const fs = require('fs');
const path = require('path');

const router = new Router();

// 当前目录路径
const routesDir = __dirname;

fs.readdirSync(routesDir).forEach(file => {
  const filePath = path.join(routesDir, file);

  // 跳过当前 index.js 本身
  if (file === 'index.js') return;

  // 只加载 .js 文件
  if (fs.statSync(filePath).isFile() && path.extname(file) === '.js') {
    const routeModule = require(filePath);
    if (routeModule && routeModule.routes && routeModule.allowedMethods) {
      router.use(routeModule.routes(), routeModule.allowedMethods());
    }
  }
});

module.exports = router;
